<?php

namespace app\manage\controller;

use app\model\ChargeModel;
use think\Db;
use think\Request;
use app\model\StudentModel;
class Student extends Base
{
    protected $request;
    protected $model;
    public function __construct(Request $request)
    {
        parent::__construct();
		$this->request = $request;
        $this->model = new StudentModel();
        $this->assign("data",$this->request->param());
   
    }
    //添加缴费页面
    public function addEntry(){
        
        
        return view("add");
    } 
    
    //学员列表
    public function studentMaterial(){
        
        $list = $this->model->alias('a')->order('a.id','desc')->where(function($query){
            //等级
            $level = $this->request->param('admin_auth')['level'];
            if($level == 4){//业务员
                $query->where("account_id",$this->request->param('admin_auth')['auth_admin_id']);
            }
            if($level != 1 && $level != 3){
                //公司id
                $cid = $this->request->param('admin_auth')['cid'];
                $query->where("cid",$cid);
            }
            //关键字
            $keyword = input("keyword");
            if(!empty($keyword)){
                $query->where("user_name|phone",'like','%'.$keyword.'%');
            }
            //业务员
            $nickname = input("nickname");
            if(!empty($nickname)){
                $query->where("account_name",'like','%'.$nickname.'%');
            }
            //公司名称
            $company_name = input("company_name");
            if(!empty($company_name)){
                $query->where("company_name",'like','%'.$company_name.'%');
            }
            //学校名称
            $school_name = input("school_name_l");
            if(!empty($school_name)){
                $query->where("school_name",'like','%'.$school_name.'%');
            }
            //编号
            $order_sn = input("order_sn");
            if(!empty($order_sn)){
                $query->where("order_sn",'like','%'.$order_sn.'%');
            }
            //身份证
            $identity_card = input("identity_card");
            if(!empty($keyword)){
                $query->where("identity_card",'like','%'.$identity_card.'%');
            }
            //报考类型
            $type = input("type");
            if(!empty($type)){
                $query->where("type",$type);
            }
            //创建时间区间
            $start = input("start",'');
            $end = input("end",'');
            if(!empty($start) && !empty($end)){
                $query->where("create_time",'between',[$start,$end]);
            }
        })
        ->join('mm_education_type b','a.type = b.id')
        ->field('a.id,a.company_name,a.order_sn,a.account_name,a.user_name,a.identity_card,a.phone,a.school_name,a.professional,a.address,a.create_time,b.title,a.hierarchy')
        ->paginate(15,false,['query' =>$this->request->param()]);
        foreach ($list as $key => $value) {
            $list[$key]->state = ChargeModel::getPrice($value->order_sn);
            
        }
        $page = $list->render();
        $this->assign("list",$list);
        $this->assign('page', $page);
        //报考类型
        $type = Db::name("education_type")->order('id','desc')->select();
        
        $this->assign("type",$type);
        
        return view("listStudent");
    }
    //添加学员
    public function ajaxAddStudent(){
      
        $data = $this->model->add($this->request->param());

        return ['msg'=>$data['msg'],'code'=>$data['code']];
    }
    public function ajaxType(){
        $pid = input("pid",'');
        return Db::name("level")->where('pid',$pid)->select();
    }








}